/* BIS.do (STATA)
	Construct BIS data.
	by Ralph Koijen & Motohiro Yogo */

#delimit ;
clear all;
set more off;
set type double;

cap log close;
log using BIS, replace;

/* Define local and global variables */

local directory = "../../Data/BIS/Debt securities statistics";

do global;


/* Step 1: Construct domestic debt securities */

/* Load data */

u ISSUER_RES ISSUER_BUS_IMM market ISSUE_OR_MAT measure
	_${year_min}_Q1-_${year_max}_Q4
	if inlist(ISSUER_BUS_IMM,"2","B","J")	/* 2 = General government, B = Financial corporations, J = Nonfinancial coporations */
	& market=="A"							/* Domestic market */
	& measure=="I"							/* Amounts outstanding */
	using "`directory'/WS_DEBT_SEC2_PUB_csv_col", clear;

drop market measure;

/* Rename variables */

rename ISSUER_RES ISO2;
rename ISSUER_BUS_IMM issuer;

foreach var of varlist *_Q* {;
	rename `var' dds`=tq(`=substr("`var'",2,.)')';
};

/* Merge country code */

merge m:1 ISO2 using Countries,
	keepusing(Counterpart)
	nogen keep(match);

drop ISO2;

/* Construct variables */

gen byte type = 0 if ISSUE_OR_MAT=="A";	/* All debt */
replace type = 1 if ISSUE_OR_MAT=="C";	/* ST debt */
replace type = 2 if ISSUE_OR_MAT=="K";	/* LT debt */

drop ISSUE_OR_MAT;

/* Reshape in long format */

reshape long dds, i(Counterpart issuer type) j(date);

/* Fix variables */

recode dds (0 = .);

/* Expand to ST and LT debt */

sort date Counterpart issuer type;
by date Counterpart issuer: gen byte obs = min(type[_n+1],3)-type;

expand obs if obs==2, gen(duplicate);

replace type = type+1 if duplicate;
replace dds = . if duplicate;

drop obs duplicate;

/* Construct ratio between ST and LT debt */

sort date Counterpart issuer type;
by date Counterpart issuer: gen ratio = dds/dds[_n+1] if type==1 & type[_n+1]==2;		/* ST to LT debt */
by date Counterpart issuer: replace ratio = dds/dds[_n-1] if type==2 & type[_n-1]==1;	/* LT to ST debt */

/* Extrapolate forward */

sort Counterpart issuer type date;
by Counterpart issuer type: replace ratio = ratio[_n-1] if missing(ratio);

/* Extrapolate backward */

gsort Counterpart issuer type -date;
by Counterpart issuer type: replace ratio = ratio[_n-1] if missing(ratio);

/* Impute ST and LT debt based on their ratio at other dates */

sort date Counterpart issuer type;
by date Counterpart issuer: replace dds = ratio*dds[_n+1] if type==1 & type[_n+1]==2 & missing(dds);	/* ST debt */
by date Counterpart issuer: replace dds = ratio*dds[_n-1] if type==2 & type[_n-1]==1 & missing(dds);	/* LT debt */

drop ratio;

/* Correct total */

egen Tdds = total(dds*(type!=0)), missing by(date Counterpart issuer);

replace dds = Tdds if type==0 & Tdds>0;

drop Tdds;

/* Keep year-end data */

gen int year = year(dofq(date));

keep if quarter(dofq(date))==4;

drop date;

/* Aggregate across issuers */

collapse (sum) dds, by(year Counterpart type) fast;

/* Impute ST and LT debt from All debt */

egen dds0 = total(dds*(type==0)), missing by(year Counterpart);
egen Tdds = total(dds*(type!=0)), missing by(year Counterpart);

replace dds = dds0*dds/Tdds if type!=0;

drop dds0 Tdds;

/* Construct variables */

gen currency = "ALL";

/* Save data */

tempfile DDS;
save `DDS';


/* Step 2: Construct total debt securities */

/* Load data */

u ISSUER_RES ISSUER_BUS_IMM market measure
	_${year_min}_Q4-_${year_max}_Q4
	if ISSUER_BUS_IMM=="1"	/* All issuers */
	& market=="1"			/* All markets */
	& measure=="I"			/* Amounts outstanding */
	using "`directory'/WS_DEBT_SEC2_PUB_csv_col", clear;

drop ISSUER_BUS_IMM market measure *_Q1 *_Q2 *_Q3;

/* Rename variables */

rename ISSUER_RES ISO2;

foreach var of varlist *_Q4 {;
	rename `var' tds`=substr("`var'",2,4)';
};

/* Merge country code */

merge m:1 ISO2 using Countries,
	keepusing(Counterpart)
	nogen keep(match);

drop ISO2;

/* Reshape in long format */

reshape long tds, i(Counterpart) j(year);

drop if missing(tds);

/* Construct variables */

gen byte type = 0;
gen currency = "ALL";

/* Save data */

tempfile TDS;
save `TDS';


/* Step 3: Construct international debt securities */

/* Load data */

u ISSUER_RES ISSUER_NAT ISSUER_BUS_IMM ISSUER_BUS_ULT market
	ISSUE_CUR_GROUP ISSUE_CUR ISSUE_OR_MAT ISSUE_RE_MAT ISSUE_RATE measure
	_${year_min}_Q4-_${year_max}_Q4
	if ISSUER_NAT=="3P"										/* All countries excluding residents */
	& ISSUER_BUS_IMM=="1"									/* All issuers */
	& ISSUER_BUS_ULT=="1"									/* All issuers */
	& market=="C"											/* International markets */
	& (inlist(ISSUE_CUR_GROUP,"A","D") & ISSUE_CUR=="TO1"	/* A = All currencies, D = Domestic currency, F = Foreign currencies */
	| ISSUE_CUR_GROUP=="F")
	& ISSUE_RE_MAT=="A"										/* All remaining maturities */
	& ISSUE_RATE=="A"										/* All rate types */
	& measure=="I"											/* Amounts outstanding */
	using "`directory'/WS_DEBT_SEC2_PUB_csv_col", clear;

drop ISSUER_NAT ISSUER_BUS_IMM ISSUER_BUS_ULT market ISSUE_RE_MAT ISSUE_RATE measure *_Q1 *_Q2 *_Q3;

/* Rename variables */

rename ISSUER_RES ISO2;

foreach var of varlist *_Q4 {;
	rename `var' ids`=substr("`var'",2,4)';
};

/* Merge country code */

merge m:1 ISO2 using Countries,
	keepusing(Counterpart Yeuro)
	nogen keep(match);

drop ISO2;

/* Construct variables */

gen byte type = 0 if ISSUE_OR_MAT=="A";	/* All debt */
replace type = 1 if ISSUE_OR_MAT=="C";	/* ST debt */
replace type = 2 if ISSUE_OR_MAT=="K";	/* LT debt */

drop ISSUE_OR_MAT;

/* Reshape in long format */

reshape long ids, i(Counterpart type ISSUE_CUR_GROUP ISSUE_CUR) j(year);

/* Fix variables */

recode ids (. = 0);

/* Construct currency */

gen currency = "ALL" if ISSUE_CUR_GROUP=="A";							/* All currencies */
replace currency = Counterpart if ISSUE_CUR_GROUP=="D" & year<Yeuro;	/* Domestic currency */
replace currency = "EUR" if ISSUE_CUR_GROUP=="D" & year>=Yeuro;
replace currency = "EUR" if ISSUE_CUR_GROUP=="F" & ISSUE_CUR=="EU1";	/* Euro */
replace currency = "USA" if ISSUE_CUR_GROUP=="F" & ISSUE_CUR=="USD";	/* US dollar */

drop if ISSUE_CUR_GROUP=="F" & ISSUE_CUR=="EU1" & year>=Yeuro;

drop Yeuro;

/* Construct other foreign currencies */

egen Tids = total(ids*(ISSUE_CUR!="TO1")) if ISSUE_CUR_GROUP=="F", missing by(year Counterpart type);

replace currency = "_OC" if ISSUE_CUR_GROUP=="F" & ISSUE_CUR=="TO1";
replace ids = max(ids-Tids,0) if ISSUE_CUR_GROUP=="F" & ISSUE_CUR=="TO1";

drop ISSUE_CUR_GROUP ISSUE_CUR Tids;

/* Correct rounding error in total */

egen Tids = total(ids*(currency!="ALL")), missing by(year Counterpart type);

replace ids = Tids if currency=="ALL";

drop Tids;

/* Save data */

tempfile IDS;
save `IDS';


/* Step 4: Construct debt outstanding */

/* Load domestic debt securities */

u `DDS', clear;

/* Merge total debt securities */

merge 1:1 year Counterpart type currency using `TDS',
	nogen;

/* Merge international debt securities */

merge 1:1 year Counterpart type currency using `IDS',
	nogen;

/* Merge country code */

merge m:1 Counterpart using Countries,
	keepusing(Ynat Idds Itds)
	nogen keep(match);

/* Keep sample of countries */

keep if year>=Ynat;

drop Ynat;

/* Update missing domestic debt securities */

replace Idds = 0 if missing(dds) & !missing(tds) & Itds==0;
replace dds = max(tds-ids,0) if missing(dds) & !missing(tds) & Itds==0;

drop tds Itds;

/* Convert to US$ billion */

foreach var of varlist dds ids {;
	replace `var' = `var'/1e3;
};

/* Merge market-to-book value */

merge m:1 year Counterpart type using Data1,
	keepusing(mb)
	nogen keep(master match);

/* Convert book value to market value */

replace dds = dds*mb if type!=0 & Idds==0;
replace ids = ids*mb if type!=0;

drop Idds mb;

/* Correct total */

foreach var of varlist dds ids {;
	egen T`var' = total(`var'*(type!=0)) if currency=="ALL", missing by(year Counterpart);

	replace `var' = T`var' if type==0 & currency=="ALL" & T`var'>0;

	drop T`var';
};

/* Label variables */

order year Counterpart type currency;

label var year		"Year";
label var type		"Asset type";
label var currency	"Currency";

label var dds		"Domestic debt securities (US$ billion)";
label var ids		"International debt securities (US$ billion)";

label define type_label
	0 "All debt"
	1 "Short-term debt"
	2 "Long-term debt", replace;

label val type type_label;

/* Save data */

sort year Counterpart type currency;

save BIS, replace;

log close;
